import { LayerManager } from "./LayerManager";
import { Global } from "../../../com/gengine/global/Global";
import { IView } from "../../mvc/interfaces/IView";
//class PopupManager
    
    export  class PopupManager
    {
        

        public static addPopUp(arg1: Object /* flash.display.DisplayObject */, arg2: string="window", arg3: Boolean=false): void
        {
            if (arg1 && LayerManager.windowLayer.contains(arg1) == false) 
            {
                arg1.x = (Global.stage.stageWidth - arg1.width) / 2;
                arg1.y = (Global.stage.stageHeight - arg1.height) / 2;
                if (arg2 != "window") 
                {
                    if (typeof arg1 === "iview") 
                    {
                        (arg1 as IView).show();
                    }
                }
                else 
                {
                    LayerManager.windowLayer.addChild(arg1);
                }
                PopupManager._topDisplayObject = arg1;
                if (arg3) 
                {
                    PopupManager.createTempCover(arg1, 0, 0, true);
                }
            }
            return;
        }

        public static centerPopup(arg1: Object /* flash.display.DisplayObject */): void
        {
            if (arg1) 
            {
                arg1.x = (Global.stage.stageWidth - arg1.width) / 2;
                arg1.y = (Global.stage.stageHeight - arg1.height) / 2;
            }
            return;
        }

        public static isTop(arg1: Object /* flash.display.DisplayObject */): Boolean
        {
            return LayerManager.windowLayer.isTop(arg1);
        }

        public static removePopup(arg1: Object /* flash.display.DisplayObject */): void
        {
            if (LayerManager.windowLayer.contains(arg1)) 
            {
                LayerManager.windowLayer.removeChild(arg1);
                if (PopupManager._topDisplayObject == arg1) 
                {
                    PopupManager._topDisplayObject = null;
                }
            }
            return;
        }

        public static isPopup(arg1: Object /* flash.display.DisplayObject */): Boolean
        {
            return LayerManager.windowLayer.contains(arg1);
        }

        public static setTop(arg1: Object /* flash.display.DisplayObject */): void
        {
            if (PopupManager._topDisplayObject == arg1) 
            {
                return;
            }
            PopupManager._topDisplayObject = arg1;
            LayerManager.windowLayer.setTop(arg1);
            return;
        }

        /* internal  */static createTempCover(arg1: Object /* flash.display.DisplayObject */, arg2: uint=0, arg3: number=0.5, arg4: Boolean=false): void
        {
            var v: Object /* flash.display.DisplayObject */;
            var color: uint=0;
            var alpha: number=0.5;
            var mouseEnabled: Boolean=false;
            var parent: Object /* flash.display.DisplayObjectContainer */;
            var back: Object /* flash.display.Sprite */;
            var rect: Object /* flash.geom.Rectangle */;
            var removeHandler: Function;

            var loc1;
            parent = null;
            back = null;
            removeHandler = null;
            v = arg1;
            color = arg2;
            alpha = arg3;
            mouseEnabled = arg4;
            removeHandler = (arg1: Object /* flash.events.Event */): void
            {
                parent.removeEventListener(flash.events.Event.REMOVED_FROM_STAGE, removeHandler);
                if (parent.contains(back)) 
                {
                    parent.removeChild(back);
                }
                return;
            }
            parent = v.parent;
            back = new Object /* flash.display.Sprite */();
            rect = PopupManager.localRectToContent(new Object /* flash.geom.Rectangle */(0, 0, parent.stage.stageWidth, parent.stage.stageHeight), parent.stage, parent);
            back.graphics.beginFill(color, alpha);
            back.graphics.drawRect(rect.x, rect.y, rect.width, rect.height);
            back.graphics.endFill();
            back.mouseEnabled = mouseEnabled;
            back.cacheAsBitmap = true;
            parent.addChildAt(back, parent.getChildIndex(v));
            v.addEventListener(flash.events.Event.REMOVED_FROM_STAGE, removeHandler);
            return;
        }

        /* internal  */static localRectToContent(arg1: Object /* flash.geom.Rectangle */, arg2: Object /* flash.display.DisplayObject */, arg3: Object /* flash.display.DisplayObject */): Object /* flash.geom.Rectangle */
        {
            if (arg2 == arg3) 
            {
                return arg1;
            }
            var loc1=PopupManager.localToContent(arg1.topLeft, arg2, arg3);
            var loc2=PopupManager.localToContent(arg1.bottomRight, arg2, arg3);
            return new Object /* flash.geom.Rectangle */(loc1.x, loc1.y, loc2.x - loc1.x, loc2.y - loc1.y);
        }

        public static localToContent(arg1: Object /* flash.geom.Point */, arg2: Object /* flash.display.DisplayObject */, arg3: Object /* flash.display.DisplayObject */): Object /* flash.geom.Point */
        {
            if (arg3 && arg2) 
            {
                return arg3.globalToLocal(arg2.localToGlobal(arg1));
            }
            if (arg2) 
            {
                return arg2.localToGlobal(arg1);
            }
            if (arg3) 
            {
                return arg3.globalToLocal(arg1);
            }
            return null;
        }

        public static closeTopWindow(): void
        {
            var loc4=null;
            var loc1=LayerManager.windowLayer;
            var loc2=loc1.numChildren;
            if (loc2 == 0) 
            {
                return;
            }
            var loc3;
            --loc3;
            while (loc3 >= 0) 
            {
                if (loc4 = loc1.getChildAt(loc3) as IView) 
                {
                    loc4.hide();
                    break;
                }
                --loc3;
            }
            return;
        }

        /* internal */ static /* var */_topDisplayObject: Object /* flash.display.DisplayObject */;
    }
